// app.js
App({
    onLaunch() {
      // 展示本地存储能力
      console.log('---onlaunch---');
      var self=this;   
      this.globalData.updateStatus=1;
      const capsule = wx.getMenuButtonBoundingClientRect(); 
      self.globalData.capsule=capsule; 
      console.log(capsule);  
     var res = wx.getWindowInfo();
        
          console.log(res);
          if (res.screenHeight - res.windowHeight - res.statusBarHeight - 32 > 72) {
            self.globalData.isFullSucreen = true
          }
          let navHeight = res.statusBarHeight
          if(res.platform == 'ios' || res.platform=='devtools'){
            self.globalData.platform = 'ios';            
          }else if(res.platform == 'android'){
            self.globalData.platform = 'android';
          }
          self.globalData.pixelRatio = res.pixelRatio;
          self.globalData.navHeight = capsule.height;
          self.globalData.navTitleTop = capsule.top;
          self.globalData.navWidth = capsule.width;
          self.globalData.navLeft = res.windowWidth - capsule.right;
          self.globalData.statusBarHeight=res.statusBarHeight;
          self.globalData.windowHeight=res.windowHeight;
          self.globalData.screenHeight=res.screenHeight;
          self.globalData.windowWidth=res.windowWidth-2;
          self.globalData.screenWidth=res.screenWidth-2;
          
        
      
      this.autoUpdate().then(res=>{ 
        // console.log(11); 
         console.log(res);
       //  that.wxUpdate(); //that.wxLogIn();  
      })
  
    },
    autoUpdate:function(){
      var that = this;
      return new Promise(function(resolve, reject) {
        let startParamObj = wx.getLaunchOptionsSync(); 
        console.log('---autoUpdate---');
        console.log('startParamObj=>'+JSON.stringify(startParamObj));
        if(wx.canIUse('getUpdateManager') && startParamObj.scene!=1154){//获取小程序更新机制兼容 
         const updateManager = wx.getUpdateManager();
        //判断改小程序是否有新版本
        updateManager.onCheckForUpdate(function(res){
            console.log(res);
          //如果有新版本，更新
          if(res.hasUpdate){
            //准备更新
            updateManager.onUpdateReady(function(){
              wx.showModal({
                title: '更新提示',
                content: '检测到新版本，是否下载新版本并重启小程序',
              success:function(res){
                if(res.confirm){
                   that.downLoadAndUpdate()         
                }else if(res.cancel){
                    wx.showModal({
                        title: '提示',
                        content: '本次版本更新涉及到新功能的添加，旧版本将无法正常使用',
                        showCancel: false, // 隐藏取消按钮
                        confirmText: '确认更新', // 只保留更新按钮
                        success: function (res) {
                          if (res.confirm) {
                            // 下载新版本，重启应用
                            that.downLoadAndUpdate(updateManager)
                          }
                        }
                      })
           
                }
              }
              })
            })
        
          }else{
           //console.log('=====>no update');
           resolve('=====>no update');
          }
        })
        }
      })
    },
      // 下载小程序最新版本并重启
  downLoadAndUpdate: function (updateManager) {
    wx.showLoading();
    // 静默下载更新小程序新版本，onUpdateReady：当新版本下载完成回调
    updateManager.onUpdateReady(function () {
      wx.hideLoading()
      // applyUpdate：强制当前小程序应用上新版本并重启
      updateManager.applyUpdate()
    })
    // onUpdateFailed：当新版本下载失败回调
    updateManager.onUpdateFailed(function () {
      // 下载新版本失败
      wx.showModal({
        title: '已有新版本',
        content: '新版本已经上线了，请删除当前小程序，重新搜索打开',
      })
    })
  },
   goto:function(url='#',index='#'){
    console.log(url);
    
    if(url!="#"){

    if(index=='index'){
     wx.reLaunch({
       url: url,
     })
    }else{
        wx.navigateTo({
          url: url,
        })
    }
    }
   
    }, 
    showModals:function(msg,url,index){
        let that = this;
        wx.showModal({
        // cancelColor: 'cancelColor',
        title:'系统提示',
        content:msg,
        //showCancel:false,
        success:function(e){
            if (e.confirm) {
            that.goto(url,index);
            }
        }
        

        })

    },
    call(phone){
        wx.makePhoneCall({
          phoneNumber: phone,
        })
    },
   request(url,data,type='get'){
    let that = this;
    return new Promise(function(resolve, reject) {
       console.log(data)  
        wx.request({
          url: that.globalData.domain+url,
          data:data,
          method:type,
          success:res=>{
              if(res.statusCode == 200){
                let d = '2024';
                resolve(res.data);
              }else{
                   that.showModals('网络错误！');
              }
          }
        })
        //resolve();
    })
    },

    globalData: {
      domain:'https://ty.lypb.cn',
      iUrl:'https://img.lypb.cn/weifen/',

    }
  })
  
